/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_hal_test_entry.h"
#include "sle_loader_hal.h"
#include "sle_seek_hal.h"
#include "sle_hal_errcode.h"

SleSeekHalCallbacks *g_sleSeekHalCbs;

void OnSeekHalUtEnableSeekCallback(SleHalErrorCode errCode)
{
    SLE_UT_LOG("[SleSeekHalTest][PASSED] OnSeekHalUtEnableSeekCallback(%d)\n", errCode);
}

void OnSeekHalUtDisableSeekCallback(SleHalErrorCode errCode)
{
    SLE_UT_LOG("[SleSeekHalTest][PASSED] OnSeekHalUtDisableSeekCallback(%d)\n", errCode);
}

void OnSeekHalUtSeekResultCallback(SleSeekResult *pSeekResult)
{
    SLE_UT_LOG("[SleSeekHalTest][PASSED] OnSeekHalUtSeekResultCallback\n");
}

void InitSleSeekHalTestLevel0(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = InitSleSeekHal();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleSeekHalTest][FAILED] InitSleSeekHalTestLevel0 failed(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] InitSleSeekHalTestLevel0 pass");
}

void DeinitSleSeekHalTestLevel0(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = DeinitSleSeekHal();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleSeekHalTest][FAILED] DeinitSleSeekHalTestLevel0 failed(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] DeinitSleSeekHalTestLevel0 pass");
}

void SleSeekHalRegisterCallbackTestLevel0(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = SleSeekHalRegisterCallback(g_sleSeekHalCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleSeekHalTest][FAILED] SleSeekHalRegisterHostCallbackTestLevel0 failed(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleSeekHalRegisterHostCallbackTestLevel0 pass");
}

void SleSeekHalSetSeekParamTestLevel0(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    SleSeekParam param = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
        .repateFilter = false,
        .filterPolicy = SLE_SEEK_FILTER_ALL_ALLOW,
        .phys = SLE_SEEK_2M_PHY,
        .type[0] = SLE_ACTIVE_SEEK,
        .interval[0] = 100,
        .window[0] = 100,
    };
    ret = SleSeekHalSetSeekParam(&param);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleSeekHalTest][FAILED] SleSeekHalSetSeekParamTestLevel0 failed(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleSeekHalSetSeekParamTestLevel0 pass");
}

void SleSeekHalStartSeekTestLevel0(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = SleSeekHalStartSeek();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleSeekHalTest][FAILED] SleSeekHalStartSeekTestLevel0 failed(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleSeekHalStartSeekTestLevel0 pass");
}

void SleSeekHalStopSeekTestLevel0(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = SleSeekHalStopSeek();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleSeekHalTest][FAILED] SleSeekHalStopSeekTestLevel0 failed(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleSeekHalStopSeekTestLevel0 pass");
}

void SleSeekHalTestLevel0(void)
{
    SLE_UT_LOG("___SleSeekHalTestLevel0 start");
    InitSleSeekHalTestLevel0();
    SleSeekHalRegisterCallbackTestLevel0();
    SleSeekHalSetSeekParamTestLevel0();
    SleSeekHalStartSeekTestLevel0();
    SleSeekHalStopSeekTestLevel0();
    DeinitSleSeekHalTestLevel0();
    SLE_UT_LOG("___SleSeekHalTestLevel0 end");    
}

void InitSleSeekHalTestLevel1(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = InitSleSeekHal();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleSeekHalTest][FAILED] InitSleSeekHalTestLevel1 failed1(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] InitSleSeekHalTestLevel1 pass1");
    //测试重复调用
    ret = InitSleSeekHal();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleSeekHalTest][FAILED] InitSleSeekHalTestLevel1 failed2(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] InitSleSeekHalTestLevel1 pass2");
}

void DeinitSleSeekHalTestLevel1(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = DeinitSleSeekHal();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleSeekHalTest][FAILED] DeinitSleSeekHalTestLevel1 failed1(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] DeinitSleSeekHalTestLevel1 pass1");
    //测试重复调用
    ret = DeinitSleSeekHal();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleSeekHalTest][FAILED] DeinitSleSeekHalTestLevel1 failed2(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] DeinitSleSeekHalTestLevel1 pass2");
}

void SleSeekHalRegisterCallbackTestLevel1(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    //测试入参为NULL
    ret = SleSeekHalRegisterCallback(NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_ERR_INVALID_PARAM, "[SleSeekHalTest][FAILED] SleSeekHalRegisterHostCallbackTestLevel1 failed1(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleSeekHalRegisterHostCallbackTestLevel1 pass1");

    ret = SleSeekHalRegisterCallback(g_sleSeekHalCbs);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleSeekHalTest][FAILED] SleSeekHalRegisterHostCallbackTestLevel1 failed2(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleSeekHalRegisterHostCallbackTestLevel1 pass2");
}

void SleSeekHalSetSeekParamTestLevel1(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = SleSeekHalSetSeekParam(NULL);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_ERR_INVALID_PARAM, "[SleSeekHalTest][FAILED] SleSeekHalSetSeekParamTestLevel1 failed1(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleSeekHalSetSeekParamTestLevel1 pass1");
    //测试重复调用
    SleSeekParam param = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
        .repateFilter = false,
        .filterPolicy = SLE_SEEK_FILTER_ALL_ALLOW,
        .phys = SLE_SEEK_2M_PHY,
        .type[0] = SLE_ACTIVE_SEEK,
        .interval[0] = 100,
        .window[0] = 100,
    };

    ret = SleSeekHalSetSeekParam(&param);
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleSeekHalTest][FAILED] SleSeekHalSetSeekParamTestLevel1 failed2(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleSeekHalSetSeekParamTestLevel1 pass2");
}

void SleSeekHalStartSeekTestLevel1(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = SleSeekHalStartSeek();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleSeekHalTest][FAILED] SleSeekHalStartSeekTestLevel1 failed1(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleSeekHalStartSeekTestLevel1 pass");
}

void SleSeekHalStopSeekTestLevel1(void)
{
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = SleSeekHalStopSeek();
    SLE_UT_CHECK_AND_PRINT_LOG(ret == SLE_HAL_SUCCESS, "[SleSeekHalTest][FAILED] SleSeekHalStopSeekTestLevel1 failed1(%d)", ret);
    SLE_UT_LOG("[SleAnnounceHalTest][PASSED] SleSeekHalStopSeekTestLevel1 pass");
}

void SleSeekHalTestLevel1(void)
{
    SLE_UT_LOG("___SleSeekHalTestLevel1 start");
    InitSleSeekHalTestLevel1();
    SleSeekHalRegisterCallbackTestLevel1();
    SleSeekHalSetSeekParamTestLevel1();
    SleSeekHalStartSeekTestLevel1();
    SleSeekHalStopSeekTestLevel1();
    DeinitSleSeekHalTestLevel1();
    SLE_UT_LOG("___SleSeekHalTestLevel1 end");
}

SleHalErrorCode InitSleSeekHalTestEntry(void)
{
    SLE_UT_CHECK_AND_RETURN_RET_LOG(g_sleSeekHalCbs == NULL, SLE_HAL_ERR_COMMON,"[SleSeekHalTest][FAILED] g_sleSeekHalCbs is not NULL");
    g_sleSeekHalCbs = (SleSeekHalCallbacks*)malloc(sizeof(SleSeekHalCallbacks));
    g_sleSeekHalCbs->OnSeekHalEnableSeekCb = OnSeekHalUtEnableSeekCallback;
    g_sleSeekHalCbs->OnSeekHalDisableSeekCb = OnSeekHalUtDisableSeekCallback;
    g_sleSeekHalCbs->OnSeekHalSeekResultCb = OnSeekHalUtSeekResultCallback;
    SLE_UT_CHECK_AND_RETURN_RET_LOG(g_sleSeekHalCbs != NULL, SLE_HAL_ERR_DRIVER_OR_SYSTEM, "[SleAnnounceHalTest][FAILED] malloc g_sleHostHalUtCbs failed");
    return SLE_HAL_SUCCESS;
}

SleHalErrorCode DeinitSleSeekHalTestEntry(void)
{
    if(g_sleSeekHalCbs != NULL) {
        free(g_sleSeekHalCbs);
        g_sleSeekHalCbs = NULL;
    }
    return SLE_HAL_SUCCESS;
}

void SleSeekHalTestEntry(void)
{
    SLE_UT_LOG("__SleSeekHalTestEntry start");
    SleHalErrorCode ret = SLE_HAL_ERR_UNKNOWN;
    ret = InitSleSeekHalTestEntry();
    SLE_UT_CHECK_AND_RETURN(ret == SLE_HAL_SUCCESS);
    SleSeekHalTestLevel0();
    SleSeekHalTestLevel1();
    ret = DeinitSleSeekHalTestEntry();
    SLE_UT_CHECK_AND_RETURN(ret == SLE_HAL_SUCCESS);
    SLE_UT_LOG("__SleSeekHalTestEntry end");
}